<?php
// Usage: ====================================================
// php svn_history.php | mysql DATABASE_NAME
//============================================================

error_reporting(E_ALL);
$svn_url = 'http://eathena-project.googlecode.com/svn/';
// $svn_url = 'file:///svnroot/eathena/';
$save_file = getcwd() . '/svn_history.xml';


$head_rev = (int)`svn log -q -rHEAD "$svn_url" | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`;
`svn log $svn_url --xml -r 1:HEAD > $save_file`;
$svn_history = simplexml_load_file($save_file);
`rm $save_file`;

header("Content-Type: text/plain"); // so browser displays it as plain text

echo "DROP TABLE IF EXISTS `svn_history`;

CREATE TABLE `svn_history` (
  `revision` SMALLINT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
  `author` VARCHAR(30) NOT NULL DEFAULT '',
  `date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
  `msg` MEDIUMTEXT COLLATE utf8_bin NOT NULL,
  PRIMARY KEY  (`revision`)
) ENGINE=MYISAM;

INSERT INTO `svn_history` (`revision`,`author`,`date`,`msg`) VALUES 
";

foreach ($svn_history->logentry as $logentry) {
	// convert to MySQL DATETIME format
	$date = str_replace('T',' ',substr($logentry->date,0,-8));
	// (revision, 'author', 'date', 'msg'),
	echo "(".$logentry->attributes()->revision.", '".$logentry->author."', '".$date."', '".mysql_escape_string($logentry->msg)."')". 
	      ( ($logentry->attributes()->revision == $head_rev || $logentry->attributes()->revision % 1000 == 0) ? ";" : "," ) ."\n";
	// insert 1,000 rows per query
	if ($logentry->attributes()->revision % 1000 == 0 && $logentry->attributes()->revision < $head_rev)
		echo "INSERT INTO `svn_history` (`revision`,`author`,`date`,`msg`) VALUES \n";
}

exit;
?>
